package cakeorcookiexample;


import cakeorcookiexample.CakeorCookieFSMController;
import cakeorcookiexample.works.CheckDessertWork;
import cakeorcookiexample.works.GenerateAorOWork;
import cakeorcookiexample.works.GenerateCWork;
import cakeorcookiexample.works.GenerateEWork;
import cakeorcookiexample.works.GenerateIWork;
import cakeorcookiexample.works.GenerateKWork;
import cakeorcookiexample.works.GenerateOWork;
import cakeorcookiexample.works.InitWork;
import controlisolation.lifecyclefsm.LifeCycleFSM;
import java.io.File;
import java.io.FileNotFoundException;
import java.net.URL;
import java.util.logging.Level;
import java.util.logging.Logger;


public class CakeOrCookieFSM implements Runnable
{
    private final static Logger _logger = Logger.getLogger(CakeOrCookieFSM.class.getName());
    private String  _fsmSCXMLPath="c:/temp/CookieOrCakes.scxml";
    private LifeCycleFSM _lifeCycleFSM;
     
    public void init() throws Exception
    {
         
         File f=new File(_fsmSCXMLPath);
         if (!f.exists())
         {
             throw new FileNotFoundException(_fsmSCXMLPath);
         }
         
         URL url= f.toURI().toURL();
         
         setLifeCycleFSM(new LifeCycleFSM(url));
         getLifeCycleFSM().setWorkForState("Init",new InitWork());
         getLifeCycleFSM().setWorkForState("Gimme_C",new GenerateCWork());
         getLifeCycleFSM().setWorkForState("Gimme_A_or_O",new GenerateAorOWork());
         getLifeCycleFSM().setWorkForState("Gimme_O",new GenerateOWork());
         getLifeCycleFSM().setWorkForState("Gimme_K",new GenerateKWork());
         getLifeCycleFSM().setWorkForState("Gimme_I",new GenerateIWork());
         getLifeCycleFSM().setWorkForState("Gimme_E",new GenerateEWork());
         getLifeCycleFSM().setWorkForState("CheckDessert",new CheckDessertWork());

         //getLifeCycleFSM().getWorkForState("Gimme_A_or_O").setMaxRunningTime(500);
         
         getLifeCycleFSM().bindTo(new CakeorCookieFSMController());
         //_lifeCycleFSM.start();
         
         
    }
    
    
    
    @Override
    public void run() {
        Thread t=new Thread(getLifeCycleFSM());
        t.start();
        delay(100);
        
        while (getLifeCycleFSM().isRunning()) {
            try {
                Thread.sleep(250);
            } catch (InterruptedException ex) {
                _logger.log(Level.SEVERE, null, ex);
            }
        }
        getLifeCycleFSM().stop();
    }
    
     /*
    @Override
    public void handleWorkStartedEvent(WorkStartedEvent e) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public void handleWorkFinishedEvent(WorkFinishedEvent e) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public void handleWorkFailedEvent(WorkFailedEvent e) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public void handleWarningsProducedEvent(WorkWarningsEvent e) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }
*/
    public LifeCycleFSM getLifeCycleFSM() {
        return _lifeCycleFSM;
    }

    public void setLifeCycleFSM(LifeCycleFSM lifeCycleFSM) {
        this._lifeCycleFSM = lifeCycleFSM;
    }
     
    private void delay(int ms)
    {
        try {
                Thread.sleep(ms);
            } catch (InterruptedException ex) {
                _logger.log(Level.SEVERE, null, ex);
            }
    }
  
}
